package com.lemon.springboot.controller;

import com.lemon.springboot.dao.IAccountDao;
import com.lemon.springboot.entity.AccountVo;
import com.lemon.springboot.entity.Result;
import com.lemon.springboot.service.ILoginRegisterService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;


/**
 * @Author liuli
 * @Date 2020/12/6 3:57
 */
@Controller
public class LoginRegisterController {
    Logger logger = LoggerFactory.getLogger(LoginRegisterController.class);

    @Autowired
    ILoginRegisterService loginRegisterService;

    @Autowired
    IAccountDao accountDao;

    @RequestMapping("/login")
    public String login(HttpServletRequest request){
        logger.info("login ip:{}", request.getRemoteAddr());
        return "login";
    }

    @RequestMapping(value = "/logCheck", method = RequestMethod.POST)
    public @ResponseBody Result logCheck(HttpServletRequest request, HttpServletResponse response, @RequestBody AccountVo param){
        Result result = new Result();
        logger.info("logCheck ip:{} account:{}", request.getRemoteAddr(), param.getAccount());
        try {
            String account = param.getAccount();
            String password = param.getPassword();
            if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password)) {
                return result.setCodeAndMessage("200","账号或密码不能为空");

            }
            AccountVo accountVoObj = loginRegisterService.login(account, password);
            if (accountVoObj != null) {
                request.getSession().setMaxInactiveInterval(600);
                request.getSession().setAttribute("account", accountVoObj);
                return result.setCodeAndMessage("200","success");
            } else {
                return result.setCodeAndMessage("201","登录失败，账号或密码错误");
            }
        } catch (Exception e) {
            logger.error("LoginRegisterController login error.", e);
            return result.setCodeAndMessage("500",e.getMessage());
        }
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public @ResponseBody Result register(@RequestBody HashMap<String, String> param) {
        Result result = new Result();
        try {
            String password = param.get("registerInputPassword");
            String password1 = param.get("registerInputPassword1");
            String account = param.get("registerInputAccount");
            String email = param.get("email");
            if (!password.equals(password1)) {
                return result.setCodeAndMessage("201", "两次密码输入不一样");
            }
            if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password) || StringUtils.isEmpty(password1)) {
                return result.setCodeAndMessage("202", "账号或密码不能为空");
            }
            if (account.length() > 10 || password.length() > 10) {
                return result.setCodeAndMessage("203", "账号和密码长度最大为10");
            }
            if (accountDao.findAccByAcc(account) == 1) {
                return result.setCodeAndMessage("204", "账号已存在");
            }
            AccountVo accountVoObj = new AccountVo();
            accountVoObj.setAccount(account);
            accountVoObj.setPassword(password);
            accountVoObj.setMailBox(email);
            if (!loginRegisterService.register(accountVoObj)) {
                return result.setCodeAndMessage("500", "注册失败");
            }
        } catch (Exception e) {
            logger.error("register error.", e);
            return result.setCodeAndMessage("500", e.getMessage());
        }
        return result.setCodeAndMessage("200", "success");
    }

    @RequestMapping("/logout")
    public String logout (HttpServletRequest request) {
        request.getSession().removeAttribute("account");
        return "login";
    }

}
